home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / general.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-21  |  19.7 KB  |  906 lines

  1. /* general.c -- Stuff that is used by all files. */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <sys/types.h>
  24. #include <sys/param.h>
  25. #include "filecntl.h"
  26.  
  27. #include "shell.h"
  28. #if defined (USG)
  29. #include <string.h>
  30. #else
  31. #include <strings.h>
  32. #endif /* USG */
  33.  
  34. #if !defined (USG) || defined (HAVE_RESOURCE)
  35. #include <sys/time.h>
  36. #endif
  37.  
  38. #include <sys/times.h>
  39. #include "maxpath.h"
  40.  
  41. #ifndef NULL
  42. #define NULL 0x0
  43. #endif
  44.  
  45. extern int errno;
  46.  
  47. /* Make the functions index and rindex if they do not exist. */
  48. #if defined (USG) && !defined (sgi) && !defined (DGUX) && !defined (index)
  49. char *
  50. index (s, c)
  51.      char *s;
  52. {
  53.   return ((char *)strchr (s, c));
  54. }
  55. #endif /* USG && !sgi && !DGUX && !index */
  56.  
  57. #if defined (USG) && !defined (sgi) && !DGUX && !defined (rindex)
  58. char *
  59. rindex (s, c)
  60.      char *s;
  61. {
  62.   return ((char *)strrchr (s, c));
  63. }
  64. #endif /* USG && !sgi && !DUGX && !rindex */
  65.  
  66. /* **************************************************************** */
  67. /*                                    */
  68. /*           Memory Allocation and Deallocation.            */
  69. /*                                    */
  70. /* **************************************************************** */
  71.  
  72. char *
  73. xmalloc (size)
  74.      int size;
  75. {
  76.   register char *temp = (char *)malloc (size);
  77.  
  78.   if (!temp)
  79.     fatal_error ("Out of virtual memory!");
  80.  
  81.   return (temp);
  82. }
  83.  
  84. char *
  85. xrealloc (pointer, size)
  86.      register char *pointer;
  87.      int size;
  88. {
  89.   char *temp;
  90.  
  91.   if (!pointer)
  92.     temp = (char *)xmalloc (size);
  93.   else
  94.     temp = (char *)realloc (pointer, size);
  95.  
  96.   if (!temp)
  97.     fatal_error ("Out of virtual memory!");
  98.  
  99.   return (temp);
  100. }
  101.  
  102.  
  103. /* **************************************************************** */
  104. /*                                    */
  105. /*             Integer to String Conversion            */
  106. /*                                    */
  107. /* **************************************************************** */
  108.  
  109. /* Number of characters that can appear in a string representation
  110.    of an integer.  32 is larger than the string rep of 2^^31 - 1. */
  111. #define MAX_INT_LEN 32
  112.  
  113. /* Integer to string conversion.  This conses the string; the
  114.    caller should free it. */
  115. char *
  116. itos (i)
  117.      int i;
  118. {
  119.   char *buf, *p, *ret;
  120.   int negative = 0;
  121.   unsigned int ui;
  122.  
  123.   buf = xmalloc (MAX_INT_LEN);
  124.  
  125.   if (i < 0)
  126.     {
  127.       negative++;
  128.       i = -i;
  129.     }
  130.  
  131.   ui = (unsigned int) i;
  132.  
  133.   buf[MAX_INT_LEN - 1] = '\0';
  134.   p = &buf[MAX_INT_LEN - 2];
  135.  
  136.   do
  137.     *p-- = (ui % 10) + '0';
  138.   while (ui /= 10);
  139.  
  140.   if (negative)
  141.     *p-- = '-';
  142.  
  143.   ret = savestring (p + 1);
  144.   free (buf);
  145.   return (ret);
  146. }
  147.  
  148. /* Return non-zero if all of the characters in STRING are digits. */
  149. int
  150. all_digits (string)
  151.      char *string;
  152. {
  153.   while (*string)
  154.     {
  155.       if (!digit (*string))
  156.     return (0);
  157.       else
  158.     string++;
  159.     }
  160.   return (1);
  161. }
  162.  
  163. /* A function to unset no-delay mode on a file descriptor.  Used in shell.c
  164.    to unset it on the fd passed as stdin.  Should be called on stdin if
  165.    readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */
  166.  
  167. #if !defined (O_NDELAY)
  168. #  if defined (FNDELAY)
  169. #    define O_NDELAY FNDELAY
  170. #  endif
  171. #endif /* O_NDELAY */
  172.  
  173. /* Make sure no-delay mode is not set on file descriptor FD. */
  174. void
  175. unset_nodelay_mode (fd)
  176.      int fd;
  177. {
  178.   int flags, set = 0;
  179.  
  180.   if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
  181.     return;
  182.  
  183. #if defined (O_NONBLOCK)
  184.   if (flags & O_NONBLOCK)
  185.     {
  186.       flags &= ~O_NONBLOCK;
  187.       set++;
  188.     }
  189. #endif /* O_NONBLOCK */
  190.  
  191. #if defined (O_NDELAY)
  192.   if (flags & O_NDELAY)
  193.     {
  194.       flags &= ~O_NDELAY;
  195.       set++;
  196.     }
  197. #endif /* O_NDELAY */
  198.  
  199.   if (set)
  200.     fcntl (fd, F_SETFL, flags);
  201. }
  202.  
  203.  
  204. /* **************************************************************** */
  205. /*                                    */
  206. /*            Generic List Functions                */
  207. /*                                    */
  208. /* **************************************************************** */
  209.  
  210. /* Call FUNCTION on every member of LIST, a generic list. */
  211. void
  212. map_over_list (list, function)
  213.      GENERIC_LIST *list;
  214.      Function *function;
  215. {
  216.   while (list) {
  217.     (*function) (list);
  218.     list = list->next;
  219.   }
  220. }
  221.  
  222. /* Call FUNCTION on every string in WORDS. */
  223. void
  224. map_over_words (words, function)
  225.      WORD_LIST *words;
  226.      Function *function;
  227. {
  228.   while (words) {
  229.     (*function)(words->word->word);
  230.     words = words->next;
  231.   }
  232. }
  233.  
  234. /* Reverse the chain of structures in LIST.  Output the new head
  235.    of the chain.  You should always assign the output value of this
  236.    function to something, or you will lose the chain. */
  237. GENERIC_LIST *
  238. reverse_list (list)
  239.      register GENERIC_LIST *list;
  240. {
  241.   register GENERIC_LIST *next, *prev = (GENERIC_LIST *)NULL;
  242.  
  243.   while (list) {
  244.     next = list->next;
  245.     list->next = prev;
  246.     prev = list;
  247.     list = next;
  248.   }
  249.   return (prev);
  250. }
  251.  
  252. /* Return the number of elements in LIST, a generic list. */
  253. int
  254. list_length (list)
  255.      register GENERIC_LIST *list;
  256. {
  257.   register int i;
  258.  
  259.   for (i = 0; list; list = list->next, i++);
  260.   return (i);
  261. }
  262.  
  263. /* Delete the element of LIST which satisfies the predicate function COMPARER.
  264.    Returns the element that was deleted, so you can dispose of it, or -1 if
  265.    the element wasn't found.  COMPARER is called with the list element and
  266.    then ARG.  Note that LIST contains the address of a variable which points
  267.    to the list.  You might call this function like this:
  268.  
  269.    SHELL_VAR *elt = delete_element (&variable_list, check_var_has_name, "foo");
  270.    dispose_variable (elt);
  271. */
  272. GENERIC_LIST *
  273. delete_element (list, comparer, arg)
  274.      GENERIC_LIST **list;
  275.      Function *comparer;
  276. {
  277.   register GENERIC_LIST *prev = (GENERIC_LIST *)NULL;
  278.   register GENERIC_LIST *temp = *list;
  279.  
  280.   while (temp) {
  281.     if ((*comparer) (temp, arg)) {
  282.       if (prev) prev->next = temp->next;
  283.       else *list = temp->next;
  284.       return (temp);
  285.     }
  286.     prev = temp;
  287.     temp = temp->next;
  288.   }
  289.   return ((GENERIC_LIST *)-1);
  290. }
  291.  
  292. /* Find NAME in ARRAY.  Return the index of NAME, or -1 if not present.
  293.    ARRAY shoudl be NULL terminated. */
  294. int
  295. find_name_in_list (name, array)
  296.      char *name, *array[];
  297. {
  298.   int i;
  299.  
  300.   for (i=0; array[i]; i++)
  301.     if (strcmp (name, array[i]) == 0)
  302.       return (i);
  303.  
  304.   return (-1);
  305. }
  306.  
  307. /* Return the length of ARRAY, a NULL terminated array of char *. */
  308. int
  309. array_len (array)
  310.      register char **array;
  311. {
  312.   register int i;
  313.   for (i=0; array[i]; i++);
  314.   return (i);
  315. }
  316.  
  317. /* Free the contents of ARRAY, a NULL terminated array of char *. */
  318. void
  319. free_array (array)
  320.      register char **array;
  321. {
  322.   register int i = 0;
  323.  
  324.   if (!array) return;
  325.  
  326.   while (array[i])
  327.     free (array[i++]);
  328.   free (array);
  329. }
  330.  
  331. /* Append LIST2 to LIST1.  Return the header of the list. */
  332. GENERIC_LIST *
  333. list_append (head, tail)
  334.      GENERIC_LIST *head, *tail;
  335. {
  336.   register GENERIC_LIST *t_head = head;
  337.  
  338.   if (!t_head)
  339.     return (tail);
  340.  
  341.   while (t_head->next) t_head = t_head->next;
  342.   t_head->next = tail;
  343.   return (head);
  344. }
  345.  
  346. /* Some random string stuff. */
  347.  
  348. /* Remove all leading whitespace from STRING.  This includes
  349.    newlines.  STRING should be terminated with a zero. */
  350. void
  351. strip_leading (string)
  352.      char *string;
  353. {
  354.   char *start = string;
  355.  
  356.   while (*string && (whitespace (*string) || *string == '\n')) string++;
  357.  
  358.   if (string != start)
  359.     {
  360.       int len = strlen (string);
  361.       bcopy (string, start, len);
  362.       start[len] = '\0';
  363.     }
  364. }
  365.  
  366. /* Remove all trailing whitespace from STRING.  This includes
  367.    newlines.  If NEWLINES_ONLY is non-zero, only trailing newlines
  368.    are removed.  STRING should be terminated with a zero. */
  369. void
  370. strip_trailing (string, newlines_only)
  371.      char *string;
  372.      int newlines_only;
  373. {
  374.   int len = strlen (string) - 1;
  375.  
  376.   while (len >= 0)
  377.     {
  378.       if ((newlines_only && string[len] == '\n') ||
  379.           (!newlines_only && whitespace (string[len])))
  380.         len--;
  381.       else
  382.         break;
  383.     }
  384.   string[len + 1] = '\0';
  385. }
  386.  
  387.  
  388. /* Remove the last N directories from PATH.  Do not PATH blank.
  389.    PATH must contain enoung space for MAXPATHLEN characters. */
  390. static void
  391. pathname_backup (path, n)
  392.      char *path;
  393.      int n;
  394. {
  395.   register char *p;
  396.  
  397.   if (!*path)
  398.     return;
  399.  
  400.   p = path + (strlen (path) - 1);
  401.  
  402.   while (n--)
  403.     {
  404.       while (*p == '/' && p != path)
  405.     p--;
  406.  
  407.       while (*p != '/' && p != path)
  408.     p--;
  409.  
  410.       *++p = '\0';
  411.     }
  412. }
  413.  
  414. static char current_path[MAXPATHLEN];
  415.  
  416. /* Turn STRING (a pathname) into an absolute pathname, assuming that
  417.    DOT_PATH contains the symbolic location of '.'.  This always
  418.    returns a new string, even if STRING was an absolute pathname to
  419.    begin with. */
  420. char *
  421. make_absolute (string, dot_path)
  422.      char *string, *dot_path;
  423. {
  424.   register char *cp;
  425.  
  426.   if (!dot_path || *string == '/')
  427.     return (savestring (string));
  428.  
  429.   strcpy (current_path, dot_path);
  430.  
  431.   if (!current_path[0])
  432.     strcpy (current_path, "./");
  433.  
  434.   cp = current_path + (strlen (current_path) - 1);
  435.  
  436.   if (*cp++ != '/')
  437.     *cp++ = '/';
  438.  
  439.   *cp = '\0';
  440.  
  441.   while (*string)
  442.     {
  443.       if (*string == '.')
  444.     {
  445.       if (!string[1])
  446.         return (savestring (current_path));
  447.  
  448.       if (string[1] == '/')
  449.         {
  450.           string += 2;
  451.           continue;
  452.         }
  453.  
  454.       if (string[1] == '.' && (string[2] == '/' || !string[2]))
  455.         {
  456.           string += 2;
  457.  
  458.           if (*string)
  459.         string++;
  460.  
  461.           pathname_backup (current_path, 1);
  462.           cp = current_path + strlen (current_path);
  463.           continue;
  464.         }
  465.     }
  466.  
  467.       while (*string && *string != '/')
  468.     *cp++ = *string++;
  469.  
  470.       if (*string)
  471.     *cp++ = *string++;
  472.  
  473.       *cp = '\0';
  474.     }
  475.   return (savestring (current_path));
  476. }
  477.  
  478. /* Return 1 if STRING contains an absolute pathname, else 0. */
  479. int
  480. absolute_pathname (string)
  481.      char *string;
  482. {
  483.   if (!string || !*string)
  484.     return (0);
  485.  
  486.   if (*string == '/')
  487.     return (1);
  488.  
  489.   if (*string++ == '.')
  490.     {
  491.       if ((!*string) || *string == '/')
  492.     return (1);
  493.  
  494.       if (*string++ == '.')
  495.     if (!*string || *string == '/')
  496.       return (1);
  497.     }
  498.   return (0);
  499. }
  500.  
  501. /* Return 1 if STRING is an absolute program name; it is absolute if it
  502.    contains any slashes.  This is used to decide whether or not to look
  503.    up through $PATH. */
  504. int
  505. absolute_program (string)
  506.      char *string;
  507. {
  508.   return ((char *)index (string, '/') != (char *)NULL);
  509. }
  510.  
  511. /* Return the `basename' of the pathname in STRING (the stuff after the
  512.    last '/').  If STRING is not a full pathname, simply return it. */
  513. char *
  514. base_pathname (string)
  515.      char *string;
  516. {
  517.   char *p = (char *)rindex (string, '/');
  518.  
  519.   if (!absolute_pathname (string))
  520.     return (string);
  521.  
  522.   if (p)
  523.     return (++p);
  524.   else
  525.     return (string);
  526. }
  527.  
  528. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  529.    match in s1.  The compare is case insensitive. */
  530. char *
  531. strindex (s1, s2)
  532.      register char *s1, *s2;
  533. {
  534.   register int i, l = strlen (s2);
  535.   register int len = strlen (s1);
  536.  
  537.   for (i = 0; (len - i) >= l; i++)
  538.     if (strnicmp (&s1[i], s2, l) == 0)
  539.       return (s1 + i);
  540.   return ((char *)NULL);
  541. }
  542.  
  543. #if !defined (to_upper)
  544. #define lowercase_p(c) (((c) > ('a' - 1) && (c) < ('z' + 1)))
  545. #define uppercase_p(c) (((c) > ('A' - 1) && (c) < ('Z' + 1)))
  546. #define pure_alphabetic(c) (lowercase_p(c) || uppercase_p(c))
  547. #define to_upper(c) (lowercase_p(c) ? ((c) - 32) : (c))
  548. #define to_lower(c) (uppercase_p(c) ? ((c) + 32) : (c))
  549. #endif /* to_upper */
  550.  
  551. /* Compare at most COUNT characters from string1 to string2.  Case
  552.    doesn't matter. */
  553. int
  554. strnicmp (string1, string2, count)
  555.      char *string1, *string2;
  556. {
  557.   register char ch1, ch2;
  558.  
  559.   while (count) {
  560.     ch1 = *string1++;
  561.     ch2 = *string2++;
  562.     if (to_upper(ch1) == to_upper(ch2))
  563.       count--;
  564.     else break;
  565.   }
  566.   return (count);
  567. }
  568.  
  569. /* strcmp (), but caseless. */
  570. int
  571. stricmp (string1, string2)
  572.      char *string1, *string2;
  573. {
  574.   register char ch1, ch2;
  575.  
  576.   while (*string1 && *string2) {
  577.     ch1 = *string1++;
  578.     ch2 = *string2++;
  579.     if (to_upper(ch1) != to_upper(ch2))
  580.       return (1);
  581.   }
  582.   return (*string1 | *string2);
  583. }
  584.  
  585. /* Return a string corresponding to the error number E.  From
  586.    the ANSI C spec. */
  587. #if defined (strerror)
  588. #undef strerror
  589. #endif
  590.  
  591. #if !defined (HAVE_STRERROR)
  592. char *
  593. strerror (e)
  594.      int e;
  595. {
  596.   extern int sys_nerr;
  597.   extern char *sys_errlist[];
  598.   static char emsg[40];
  599.  
  600.   if (e > 0 && e < sys_nerr)
  601.     return (sys_errlist[e]);
  602.   else
  603.     {
  604.       sprintf (emsg, "Unknown error %d", e);
  605.       return (&emsg[0]);
  606.     }
  607. }
  608. #endif /* HAVE_STRERROR */
  609.  
  610. #if !defined (USG) || defined (HAVE_RESOURCE)
  611. /* Print the contents of a struct timeval * in a standard way. */
  612. void
  613. print_timeval (tvp)
  614.      struct timeval *tvp;
  615. {
  616.   int minutes, seconds_fraction;
  617.   long seconds;
  618.  
  619.   seconds = tvp->tv_sec;
  620.  
  621.   seconds_fraction = tvp->tv_usec % 1000000;
  622.   seconds_fraction = (seconds_fraction * 100) / 1000000;
  623.  
  624.   minutes = seconds / 60;
  625.   seconds %= 60;
  626.  
  627.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  628. }
  629. #endif
  630.  
  631. /* Print the time defined by a time_t (returned by the `times' and `time'
  632.    system calls) in a standard way.  This is scaled in terms of HZ, which
  633.    is what is returned by the `times' call. */
  634.  
  635. #if !defined (BrainDeath)
  636. #  if !defined (HZ)
  637. #    if defined (USG)
  638. #      define HZ 100        /* From my Sys V.3.2 manual for times(2) */
  639. #    else
  640. #      define HZ 60        /* HZ is always 60 on BSD systems */
  641. #    endif /* USG */
  642. #  endif /* HZ */
  643.  
  644. void
  645. print_time_in_hz (t)
  646.   time_t t;
  647. {
  648.   int minutes, seconds_fraction;
  649.   long seconds;
  650.  
  651.   seconds_fraction = t % HZ;
  652.   seconds_fraction = (seconds_fraction * 100) / HZ;
  653.  
  654.   seconds = t / HZ;
  655.  
  656.   minutes = seconds / 60;
  657.   seconds %= 60;
  658.  
  659.   printf ("%0dm%0d.%02ds",  minutes, seconds, seconds_fraction);
  660. }
  661. #endif /* BrainDeath */
  662.  
  663. #if !defined (HAVE_DUP2)
  664. /* Replacement for dup2 (), for those systems which either don't have it,
  665.    or supply one with broken behaviour. */
  666. int
  667. dup2 (fd1, fd2)
  668.      int fd1, fd2;
  669. {
  670.   int saved_errno, r;
  671.  
  672.   /* If FD1 is not a valid file descriptor, then return immediately with
  673.      an error. */
  674.   if (fcntl (fd1, F_GETFL, 0) == -1)
  675.     return (-1);
  676.  
  677.   if (fd2 < 0 || fd2 >= getdtablesize ())
  678.     {
  679.       errno = EBADF;
  680.       return (-1);
  681.     }
  682.  
  683.   if (fd1 == fd2)
  684.     return (0);
  685.  
  686.   saved_errno = errno;
  687.  
  688.   (void) close (fd2);
  689.   r = fcntl (fd1, F_DUPFD, fd2);
  690.  
  691.   if (r >= 0)
  692.     errno = saved_errno;
  693.   else
  694.     if (errno == EINVAL)
  695.       errno = EBADF;
  696.  
  697.   /* Force the new file descriptor to remain open across exec () calls. */
  698.   SET_OPEN_ON_EXEC (fd2);
  699.   return (r);
  700. }
  701. #endif /* !HAVE_DUP2 */
  702.  
  703. /*
  704.  * Return the total number of available file descriptors.
  705.  *
  706.  * On some systems, like 4.2BSD and its descendents, there is a system call
  707.  * that returns the size of the descriptor table: getdtablesize().  There are
  708.  * lots of ways to emulate this on non-BSD systems.
  709.  *
  710.  * On System V.3, this can be obtained via a call to ulimit:
  711.  *    return (ulimit(4, 0L));
  712.  *
  713.  * On other System V systems, NOFILE is defined in /usr/include/sys/param.h
  714.  * (this is what we assume below), so we can simply use it:
  715.  *    return (NOFILE);
  716.  *
  717.  * On POSIX systems, there are specific functions for retrieving various
  718.  * configuration parameters:
  719.  *    return (sysconf(_SC_OPEN_MAX));
  720.  *
  721.  */
  722.  
  723. #if defined (USG) || defined (HPUX)
  724. int
  725. getdtablesize ()
  726. {
  727. #  if defined (_POSIX_VERSION) && defined (_SC_OPEN_MAX)
  728.   return (sysconf(_SC_OPEN_MAX));    /* Posix systems use sysconf */
  729. #  else /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
  730. #    if defined (USGr3)
  731.   return (ulimit (4, 0L));    /* System V.3 systems use ulimit(4, 0L) */
  732. #    else /* !USGr3 */
  733. #      if defined (NOFILE)    /* Other systems use NOFILE */
  734.   return (NOFILE);
  735. #      else /* !NOFILE */
  736.   return (20);            /* XXX - traditional value is 20 */
  737. #      endif /* !NOFILE */
  738. #    endif /* !USGr3 */
  739. #  endif /* ! (_POSIX_VERSION && _SC_OPEN_MAX) */
  740. }
  741. #endif /* USG && !defined USGr4 */
  742.  
  743. #if defined (USG) && !defined (sgi)
  744.  
  745. #if !defined (RISC6000)
  746. bcopy(s,d,n) char *d,*s; { memcpy (d, s, n); }
  747. bzero(s,n) char *s; int n; { memset(s, '\0', n); }
  748. #endif /* RISC6000 */
  749.  
  750. #if !defined (HAVE_GETWD)
  751. char *
  752. getwd (string)
  753.      char *string;
  754. {
  755.   extern char *getcwd ();
  756.   char *result;
  757.  
  758.   result = getcwd (string, MAXPATHLEN);
  759.   if (result == NULL)
  760.     strcpy (string, "getwd: cannot access parent directories");
  761.   return (result);
  762. }
  763. #endif /* !HAVE_GETWD */
  764.  
  765. #if !defined (HPUX)
  766. #include <sys/utsname.h>
  767. int
  768. gethostname (name, namelen)
  769.      char *name;
  770.      int namelen;
  771. {
  772.   int i;
  773.   struct utsname ut;
  774.  
  775.   --namelen;
  776.  
  777.   uname (&ut);
  778.   i = strlen (ut.nodename) + 1;
  779.   strncpy (name, ut.nodename, i < namelen ? i : namelen);
  780.   name[namelen] = '\0';
  781.   return (0);
  782. }
  783. #endif /* !HPUX */
  784. #endif /* USG && !sgi */
  785.  
  786. /* A slightly related function.  Get the prettiest name of this
  787.    directory possible. */
  788. static char tdir[MAXPATHLEN];
  789.  
  790. /* Return a pretty pathname.  If the first part of the pathname is
  791.    the same as $HOME, then replace that with `~'.  */
  792. char *
  793. polite_directory_format (name)
  794.      char *name;
  795. {
  796.   char *home = get_string_value ("HOME");
  797.   int l = home ? strlen (home) : 0;
  798.  
  799.   if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/'))
  800.     {
  801.       strcpy (tdir + 1, name + l);
  802.       tdir[0] = '~';
  803.       return (tdir);
  804.     }
  805.   else
  806.     return (name);
  807. }
  808.  
  809. #if defined (USG) || (defined (_POSIX_VERSION) && defined (Ultrix))
  810. int
  811. sysv_getc (stream)
  812.      FILE *stream;
  813. {
  814.   int result;
  815.   char c;
  816.  
  817.   while (1)
  818.     {
  819.       result = read (fileno (stream), &c, sizeof (char));
  820.  
  821.       if (result == 0)
  822.     return (EOF);
  823.  
  824.       if (result == sizeof (char))
  825.     return (c);
  826.  
  827.       if (errno != EINTR)
  828.     return (EOF);
  829.     }
  830. }
  831.  
  832. /* USG and POSIX systems do not have killpg ().  But we use it in
  833.    jobs.c, nojobs.c and builtins.c. */
  834. #if !defined (_POSIX_VERSION)
  835. #define pid_t int
  836. #endif /* _POSIX_VERSION */
  837.  
  838. int
  839. killpg (pgrp, sig)
  840.      pid_t pgrp;
  841.      int sig;
  842. {
  843.   int result;
  844.  
  845.   result = kill (-pgrp, sig);
  846.   return (result);
  847. }
  848. #endif /* USG  || _POSIX_VERSION */
  849.  
  850. /* **************************************************************** */
  851. /*                                    */
  852. /*            Tilde Initialization and Expansion            */
  853. /*                                    */
  854. /* **************************************************************** */
  855.  
  856. /* If tilde_expand hasn't been able to expand the text, perhaps it
  857.    is a special shell expansion.  This function is installed as the
  858.    tilde_expansion_failure_hook.  It knows how to expand ~- and ~+. */
  859. static char *
  860. bash_tilde_expand (text)
  861.      char *text;
  862. {
  863.   char *result = (char *)NULL;
  864.  
  865.   if (strcmp (text, "-") == 0)
  866.     result = get_string_value ("OLDPWD");
  867.   else if (strcmp (text, "+") == 0)
  868.     result = get_string_value ("PWD");
  869.  
  870.   if (result)
  871.     result = savestring (result);
  872.  
  873.   return (result);
  874. }
  875.  
  876. /* Initialize the tilde expander.  In Bash, we handle `~-' and `~+', as
  877.    well as handling special tilde prefixes; `:~" and `=~' are indications
  878.    that we should do tilde expansion. */
  879. void
  880. tilde_initialize ()
  881. {
  882.   extern Function *tilde_expansion_failure_hook;
  883.   extern char **tilde_additional_prefixes, **tilde_additional_suffixes;
  884.   static int times_called = 0;
  885.  
  886.   /* Tell the tilde expander that we want a crack if it fails. */
  887.   tilde_expansion_failure_hook = (Function *)bash_tilde_expand;
  888.  
  889.   /* Tell the tilde expander about special strings which start a tilde
  890.      expansion, and the special strings that end one.  Only do this once.
  891.      tilde_initialize () is called from within bashline_reinitialize (). */
  892.   if (times_called == 0)
  893.     {
  894.       tilde_additional_prefixes = (char **)xmalloc (3 * sizeof (char *));
  895.       tilde_additional_prefixes[0] = "=~";
  896.       tilde_additional_prefixes[1] = ":~";
  897.       tilde_additional_prefixes[2] = (char *)NULL;
  898.  
  899.       tilde_additional_suffixes = (char **)xmalloc (3 * sizeof (char *));
  900.       tilde_additional_suffixes[0] = ":";
  901.       tilde_additional_suffixes[1] = "=~";
  902.       tilde_additional_suffixes[2] = (char *)NULL;
  903.     }
  904.   times_called++;
  905. }
  906.